import {MatSnackBar, MatSnackBarConfig} from '@angular/material';
import {HttpErrorResponse} from '@angular/common/http';

export function openSnackBar(snackBar: MatSnackBar, message: string, data?: {
  action?: string;
  config?: MatSnackBarConfig
}): void {
  if (!data) {
    data = {};
  }
  if (!data.action) {
    data.action = 'Ok';
  }
  if (!data.config) {
    data.config = {
      duration: 3000
    };
  }
  snackBar.open(message, data.action, data.config);
}

export function errorSnackBar(snackBar: MatSnackBar, error: HttpErrorResponse) {
  errSnackbar(snackBar, error.error);
}

export function errSnackbar(snackBar: MatSnackBar, msg: string) {
  openSnackBar(snackBar, '错误: ' + msg, {
    config: {
      duration: 20000
    }
  });
}
